package com.mazaiting.accessibility.base

import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.AccessibilityServiceInfo
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import com.mazaiting.accessibility.interfaces.IAccessibilityService

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-07-24
 * @description 无障碍操作工具类
 */
class AccessibilityOperator private constructor() {
    companion object {
        // 单例
        val INSTANCE: AccessibilityOperator
                by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { AccessibilityOperator() }
    }

    /**
     * 无障碍服务
     */
    private var mAccessibilityService: AccessibilityService? = null

    /**
     * 无障碍服务接口
     */
    private var mService: IAccessibilityService? = null

    /**
     * 绑定无障碍服务
     */
    fun registerAccessibilityService(service: AccessibilityService) {
        this.mAccessibilityService = service
    }

    /**
     * 解绑无障碍服务
     */
    fun unregisterAccessibilityService() {
        if (null != mAccessibilityService) {
            mAccessibilityService = null
        }
    }

    /**
     * 服务连接
     */
    fun onServiceConnected() {
        setPackages()
        mAccessibilityService?.serviceInfo
    }

    /**
     * 绑定包名
     */
    private fun setPackages() {
        val info = AccessibilityServiceInfo()
        //相应时间的类型,(长安,点击,滑动)
        info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK
        //反馈给用户的类型,这里是语音
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK
        //过滤的包名
        info.packageNames = getPackageNames()
        // 设置
        mAccessibilityService?.serviceInfo = info
    }

    /**
     * 获取包名数组
     */
    private fun getPackageNames(): Array<out String>? {
        return mService?.getPackageNames()
    }

    /**
     * 无障碍事件
     * @param event 事件
     */
    fun onAccessibilityEvent(event: AccessibilityEvent) {
        mService?.onAccessibilityEvent(event)
    }

    /**
     * 设置服务
     * @param service 无障碍服务
     */
    fun setService(service: IAccessibilityService?) {
        if (null != service) {
            this.mService = service
        }
    }

    /**
     * 获取根节点
     *
     * @return
     */
    fun getRootNodeInfo(): AccessibilityNodeInfo? {
        var nodeInfo: AccessibilityNodeInfo? = null
        if (null != mAccessibilityService) {
            // 获得窗体根节点
            nodeInfo = mAccessibilityService?.rootInActiveWindow
        }
        return nodeInfo
    }
}